﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class JobPermissionService : IJobPermissionService
    {
        private IJobPermissionDal _jobp;
        private IMapper _mapper;
        public JobPermissionService(IJobPermissionDal jobp, IMapper mapper)
        {
            _jobp = jobp;
            _mapper = mapper;
        }

        public async Task<int> AddJobPermissionAsync(int staffId, string[] title)
        {
            return await _jobp.AddAsync(new Jobpermission
            {
                StaffId = staffId,
                JobsId = title.ToString()
            }) ;
        }

        public async Task<int> DeleteJobPermissionAsync(int id)
        {
            var data = await _jobp.QueryAsync(id);
            return data == null ? -1 : await _jobp.DeleteAsync(data);
        }

        public async Task<int> EditJobPermissionAsync(int id, int staffId, string[] title)
        {
            var data = await _jobp.QueryAsync(id);
            if (data == null)
                return -1;
            data.StaffId = staffId;
            data.JobsId = title.ToString();
            return await _jobp.EditAsync(data);
        }

        public async Task<List<JobPermissionDto>> QueryJobPermissionAllAsync()
        {
            var data = await _jobp.QueryAsync();
            return _mapper.Map<List<Jobpermission>,List<JobPermissionDto>>(data);
        }

        public async Task<JobPermissionDto> QueryJobPermissionByIdAsync(int id)
        {
            var data = await _jobp.QueryAsync(id);
            return _mapper.Map<Jobpermission,JobPermissionDto>(data);
        }
    }
}
